﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace AdministrationWeb.Administration
{
    public partial class BulkDeleteUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnDeleteAllStudents_Click(object sender, EventArgs e)
        {
            string connectionString = "ApplicationConnectionString";

            string deleteStatement = "DELETE FROM users WHERE permission != 'A';";

            MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[connectionString].ConnectionString);
            MySqlCommand command = new MySqlCommand(deleteStatement, conn);

            try
            {
                conn.Open();
                int numRows = command.ExecuteNonQuery();

                lbl_Error.Text = "Deleted All " + numRows + " Students Successfully!";
                txtYear.Text = "";
            }
            catch (Exception ex)
            {
                lbl_Error.Text = "Error Deleting Students - " + ex;
            }
            finally
            {
                conn.Close();
            }
        }

        protected void btnDeleteStudentsByYear_Click(object sender, EventArgs e)
        {
            string connectionString = "ApplicationConnectionString";

            string deleteStatement = "DELETE FROM users WHERE permission != 'A' AND graduation_year = ?year;";

            MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[connectionString].ConnectionString);
            MySqlCommand command = new MySqlCommand(deleteStatement, conn);

            command.Parameters.AddWithValue("?year", txtYear.Text);

            try
            {
                conn.Open();
                int numRows = command.ExecuteNonQuery();

                lbl_Error.Text = "Deleted " + numRows + " Students from " + txtYear.Text + " Successfully!";
                txtYear.Text = "";
            }
            catch (Exception ex)
            {
                lbl_Error.Text = "Error Deleting Students - " + ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}